# vim: ts=4:sw=4:syntax=perl

use gri_secmgr qw(get_cookie_value got_cookie);
use gri_database;
use Data::Dumper;
use MIME::Base64;

# Here we get the username and then work out the groups, and then
# work out the groups they are allowed to view...
#
# Then we get the list are work out the structure of them ...
# though for the first iteration we simply put them in a list!!!
#

use Data::Dumper;

sub mm_docrepos_sections_chooser_populate {
my $optional=shift;
my $all_portals=shift;
my $all_sections=shift;

	if(!defined($optional) || $optional eq "?optional - not defined?") {
		$optional="no";
	}
	if(!defined($all_portals) || $all_portals eq "?all_portals - not defined?") {
		$all_portals="0";
	}
	if(!defined($all_sections) || $all_sections eq "?all_sections - not defined?") {
		$all_sections="0";
	}

	my $v=get_cookie_value(name=>"gri_dbm_intranet");
	$v=~ s/\s+.*//;
	my $username=$v;

	my $db_dir=$::GRI_FRONTEND->get_global_directory("database");
	my $docrepos=$::GRI_FRONTEND->get_global_directory("docrepos");
	my $DB=new gri_database("$db_dir/gri.db");

	# Special handling if sections area is completely empty and 
	my $setup_init_docrepos=$::GRI_FRONTEND->get_variable("SETUP_INIT_DOCREPOS");
	if(!defined($setup_init_docrepos)) {
		$setup_init_docrepos=0;
	}
	if($setup_init_docrepos && $DB->get_section_count()==0) {
		# No sections defined, so give indication of this...
		# Give link if administrator, otherwise message
		my ($a,$b)=$::GRI_FRONTEND->does_user_have_role(undef,'docrepos_admin');
		if($a==1) {
			my $l=link_to_str("stdlink","Setup Document Repository","form_docrepos_admin_add_group","");
			return '<br><div style="position: relative; top: 20px; width: 600px; margin: auto; background-color: red; color: white;"><br><div style="text-align: center;">No document repositories are yet defined - click ' . $l . ' to set one up.<br>&nbsp;</div></div>';
		}
		return '<div style="position: relative; top: 20px; width: 600px; margin: auto; background-color: red; color: white;"><br><div style="text-align: center;">No document repositories are yet defined - please contact your site administrator.<br>&nbsp;</div></div>';
	}
	# If no document repositories are set up then don't shown anything 
	# currently shows an empty list.

	if($DB->get_section_count()==0) {
		return "";
	}
	# Get list of groups that the user belongs too...
	my ($a,$b)=$DB->get_user_sections(user=>$username,type=>"reader");
	my ($a2,$b2)=$DB->get_user_sections(user=>$username,type=>"publisher");
	my ($a3,$b3);
	if($all_sections eq "1") {
		($a3,$b3)=$DB->list_sections();
	}
	if($a!=1) {
		if($optional eq "optional") { return ""; }
		return "ERROR: Unable to get details of sections for user '$username'.";
	}
	if($a2!=1) {
		if($optional eq "optional") { return ""; }
		return "ERROR: Unable to get details of sections for user '$username'.";
	}
	# Combine the names of all sections, 
	my %SS=();
	$SS{$_}=1 for (@$b);
	$SS{$_}=1 for (@$b2);
	# return "" if !scalar(keys(%SS));
	# $b is a ref to all the sections the user can access..
	my $s="<center>";
	if($optional eq "optional") {
		$s.="<font class=stdtexttitle>Below is a list of document sections you currently have access to.<br>\n";
	}
	$s.="<table class=gri_container cols=4>\n";
	$s.="<tr class=gri_headerrow>\n";
	$s.="<td class=gri_cell width=60%>Available Document Sections</td>";
	$s.="<td class=gri_cell-right width=10%>Documents</td>";
	$s.="<td class=gri_cell-right width=15%>Most Recent</td>";
	$s.="<td class=gri_cell-right width=15%>Actions</td>";
	$s.="</tr>\n";
	my $row=0;
	my @L=keys %SS;
	if($all_sections eq "1") {
		for my $csec (@$b3) {
			if(! grep {$_ eq $csec} @L) {
				push @L,$csec;
			}
		}
	}
	my $request_access=0; my $have_all=1;
	for my $csec (@L) {
		my $class="gri_odd";
		$class="gri_even" if $row%2==0;
		$row++;
		my ($c,$d)=$DB->get_section(section=>$csec);
		if($c==1) {
			my $attrs=$d->{attributes};
			my @A=();
			if(defined($attrs)) {
				$attrs =~ s/\s+//g;
				@A=split(/,/,$attrs);
				next if grep {$_ eq "invisible"} @A;
			}
			my ($c1,$c2,$c3,$c4)=$DB->get_section_stats(section=>$csec,docrepos=>$docrepos);
			next if $c1 != 1;
			my $view_it=0;
			my $upload_it=0;
			if((grep {$_ eq $csec} @$b) || (grep {$_ eq "public" || $_ eq "publicread"} @A)) {
				$view_it=1;
			}
			if((grep {$_ eq $csec} @$b2) || (grep {$_ eq "public" || $_ eq "publicwrite"} @A)) {
				$upload_it=1;
			}
			$s.="<tr id=$class>\n";
			if($username eq "public" && $view_it==0 && $upload_it==0) {
				$s.="<td class=gri_cell>$d->{title}</td>";
			} else {
				$s.="<td class=gri_cell>$d->{title}</td>";
			}
			$s.="<td class=gri_cell-right>$c2</td>";
			my $uploaded='';
			if($c2>0) {
				my @lt=localtime(int($c4)); $uploaded=$::GRI_FRONTEND->datetime_str(\@lt);
			}
			if($username eq "public" && $view_it==0 && $upload_it==0) {
				$s.="<td class=gri_cell-right>$uploaded</td>";
			} else {
				$s.="<td class=gri_cell-right>$uploaded</td>";
			}
			$s.="<td class=gri_cell-right>";
			if($upload_it) {
				my $snam=encode_base64($csec); $snam =~ s/\n//g;
				my $lp=unpack("h*","docrepos_sections_chooser");

				my $l=link_to_str("stdlink","Upload","form_docrepos_upload_file","lp=$lp,file_section=$snam");
				$s.="$l &nbsp; &nbsp; ";
			}
			if($view_it) {
				my $snam=encode_base64($csec); $snam =~ s/\n//g;
				my $l=link_to_str("stdlink","View","docrepos_show_section_docs","section=$snam,page=1");
				$s.="$l";
			}
			# If we have no links then generate a 'Request Access' link...
			if($upload_it+$view_it==0) {
				$have_all=0;
				$request_access=1;
				my $n=string_to_hex($csec);
				my $n2=string_to_hex($v);
				my $l=link_to_str("stdlink","Request Access","document_section_access","section=$n,user=$n2");
				if($username eq "public") {
					$l="";
				}
				$s.="$l";
			}
			$s.="</td></tr>\n";
		}
	}
	if(! $have_all) {
		if($request_access==0) {
			my $class="gri_odd";
			$class="gri_even" if $row%2==0;
			my $l=link_to_str("stdlink","View all sections","_","all_portals=$all_portals,all_sections=1");
			$s.="<tr id=$class><td class=gri_cell-right colspan=4>$l</td></tr>";
		} else {
			my $class="gri_odd";
			$class="gri_even" if $row%2==0;
			my $l=link_to_str("stdlink","View accessible sections only","_","all_portals=$all_portals,all_sections=0");
			$s.="<tr id=$class><td class=gri_cell-right colspan=4>$l</td></tr>";
		}
	}
	$s.="</table></center><br>\n";
	return $s;
}

